//
//  SQHomeViewController.swift
//  SwiftWeibo
//
//  Created by suqianghotel on 16/11/14.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import UIKit



class SQHomeViewController: SQBaseViewController {
    
    fileprivate let listViewModel = SQStatusListViewModel()

    override func viewDidLoad() {
        
        super.viewDidLoad()

        
        self.tableview?.register(UINib(nibName: "SQStatusCell", bundle: nil), forCellReuseIdentifier: SQStatusCellID)
        self.tableview?.register(UINib(nibName: "SQStatusRepostCell", bundle: nil), forCellReuseIdentifier: SQRepostStatusCellID)
//        self.tableview?.estimatedRowHeight = 300;
//        self.tableview?.rowHeight = UITableViewAutomaticDimension
        self.tableview?.separatorStyle = .none
        loadData()

    }
    
    
    override func loadData() {
    
        self.tableview?.refreshControl?.beginRefreshing()
        
        let count = listViewModel.statusViewModels.count
        
        listViewModel.loadStatuses(isPullup:self.isPullup) {(isSuccess) in
            
            self.refreshControl?.endRefreshing()
            
            if self.isPullup == true && count == self.listViewModel.statusViewModels.count
            {
                self.enablePullup = false
            }
            
            
            self.tableview?.reloadData()
            self.isPullup = false
            
        }
        
    }
    
    
    override func setupVisitorView() {
        
        super.setupVisitorView()
        self.visitorView?.attentionButton.isHidden = false
        self.visitorView?.imageView.isHidden = false
        self.visitorView?.registerButton.isHidden = true
        self.visitorView?.loginButton.isHidden = true
        
        
        let registerItem = UIBarButtonItem(title:"注册",
                                           colorHighlight: UIColor.gray,
                                           colorNormal : UIColor.orange,
                                           target:self,
                                           action:#selector(SQHomeViewController.register))
        self.navigationItem.leftBarButtonItems = [UIBarButtonItem.space, registerItem]
        
        let loginItem = UIBarButtonItem(title:"登录",
                                        colorHighlight: UIColor.gray,
                                        colorNormal: UIColor.orange,
                                        target:self,
                                        action:#selector(SQHomeViewController.login))
        self.navigationItem.rightBarButtonItems = [UIBarButtonItem.space, loginItem]
        
        
       
        
        
        
    }
    
    
    
    override func setupNavigation()
    {
        
        let friendItem = UIBarButtonItem(imageName:"navigationbar_friendattention",
                                         highlighted: "navigationbar_friendattention_highlighted",
                                         target:self,
                                         action:#selector(SQHomeViewController.friends))
        self.navigationItem.leftBarButtonItems = [UIBarButtonItem.space, friendItem]
        
        
//        let next = UIBarButtonItem(title :"注册",
//                                   target:self,
//                                   action:#selector(SQHomeViewController.nextVC))
//        self.navigationItem.rightBarButtonItems = [UIBarButtonItem.space, next]
        
        
        let titleButton = SQHomeTitleButton(title:SQUserAccountManager.shared.userInfo?.screen_name,
                                            target:self,
                                            action:#selector(SQHomeViewController.titleButtonClicked))
      
        
        
        self.navigationItem.titleView = titleButton
        
       
    }
    
    @objc private func titleButtonClicked(_ btn:UIButton)
    {
        btn.isSelected = !btn.isSelected
    }
    
    
    @objc private func friends()
    {
        
    }
    
    
    @objc private func nextVC()
    {
        let vc = SQTestViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }

    
}




extension SQHomeViewController
{
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       return self.listViewModel.statusViewModels[indexPath.row].rowHeight
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listViewModel.statusViewModels.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let statusViewModel = self.listViewModel.statusViewModels[indexPath.row]

    
        
        let cell = tableview!.dequeueReusableCell(withIdentifier: statusViewModel.reuseID!, for: indexPath) as! SQStatusCell
        
        
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let statusViewModel = self.listViewModel.statusViewModels[indexPath.row]
         (cell as! SQStatusCell).statusViewModel = statusViewModel
    }
    
}

class SQHomeTitleButton: UIButton {
    
    init(title: String?,target:AnyObject, action:Selector?) {
        super.init(frame: CGRect())
        
        if title == nil {
            setTitle("首页", for: .normal)
        }
        else
        {
            setTitle(title! + " ", for: .normal)
            setImage(UIImage(named:"navigationbar_arrow_up"), for: .normal)
            setImage(UIImage(named:"navigationbar_arrow_down"), for: .selected)
        }
        
        if let action = action {
            addTarget(target, action: action, for: .touchUpInside)
        }
    
        
        setTitleColor(UIColor.black, for: .normal)
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        
        sizeToFit()
        
        layoutIfNeeded()
        
    }
    

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        guard
            let titleLabel = titleLabel,
            let imageView = imageView
        else
        {
            return;
        }
        
        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
        
    }
}











